#!/usr/bin/env bash

. ../src/cfg_modules

# color format
KRED='\x1B[0;31m'
KSALMON='\x1B[1;31m'
KGREEN='\x1B[0;32m'
KLIMEGREEN='\x1B[1;32m'
KYELLOW='\x1B[0;33m'
KORANGE='\x1B[0;34m'
KBLUE='\x1B[1;34m'
KMAGENTA='\x1B[0;35m'
KPINK='\x1B[1;35m'
KCYAN='\x1B[0;36m'
RESET='\x1B[0m'

### OPTIONS
OPT_QUIET=1

REPORT_FILE=apmtest_$(date +"%F_%T").report
TMP_LOG=tmp_$(date +"%F_%T").log

function print_usage
{
    echo "Usage: ./apmtest.sh"
    echo " -h, --help     This small usage guide"
    echo " --no-quiet     Show test process messages"
}

function report_module_status
{
    if [[ $# -lt 3 ]]; then
        echo "Missing params of report_module_status"
        exit
    fi

    module=$1
    action=$2
    status=$3

    echo -e "\n## ${action} ${module} ${status}." >> ${REPORT_FILE}
}

function test_modules
{
    for module_name in ${modules_list[@]}
    do
        >${TMP_LOG}

        echo "Test ${module_name}..."

        if [ x"${OPT_QUIET}" = x1 ]; then
            make -C ${module_name} clean >/dev/null 2>&1
            make -C ${module_name} all >${TMP_LOG} 2>&1
        else
            make -C ${module_name} clean 2>&1
            make -C ${module_name} all 2>&1 | tee -a ${TMP_LOG}
        fi

        # 1. 检查是否能够make
        egrep -q "make: \*\*\* No (rule to make target)|(targets specified and no makefile found)" ${TMP_LOG}
        if [[ $? -eq 0 ]]; then
            report_module_status ${module_name} Make failed
        else
            # 2. 检查编译是否出错
            egrep -q "make.*Error [[:digit:]]+" ${TMP_LOG}
            if [[ $? -eq 0 ]]; then
                report_module_status ${module_name} Make failed
            else
                # 3. 检查测试运行是否都正常
                ret=`cat ${TMP_LOG} | grep "Run Summary" -A 3 | grep "tests" | awk '{print $5}'`
                if [[ ${ret} -ne 0 ]]; then
                    report_module_status ${module_name} Test failed
                    cat ${TMP_LOG} | grep "Run Summary" -A 3 >> ${REPORT_FILE}
                else
                    # 4. 检查测试过程中是否有异常信息
                    cat ${TMP_LOG} | grep -q "== Invalid "
                    if [[ $? -eq 0 ]]; then
                        report_module_status ${module_name} Test failed
                        sed -n -e '/== Invalid /,/ main (/ p' ${TMP_LOG} >> ${REPORT_FILE}
                    else
                        # 5. 检查内存是否有泄漏
                        cat ${TMP_LOG} | grep -q "All heap blocks were freed -- no leaks are possible"
                        if [[ $? -eq 0 ]]; then
                            report_module_status ${module_name} Test passed
                        else
                            report_module_status ${module_name} Test failed
                            cat ${TMP_LOG} | grep "HEAP SUMMARY:" -A 99999 | sed "s|^==[[:digit:]]\+==[[:space:]]||" >> ${REPORT_FILE}
                        fi
                    fi
                fi
            fi
        fi
    done
}




#-o或--options选项后面接可接受的短选项，如ab:c::，表示可接受的短选项为-a -b -c，其中-a选项不接参数，-b选项后必须接参数，-c选项的参数为可选的
#-l或--long选项后面接可接受的长选项，用逗号分开，冒号的意义同短选项。
#-n选项后接选项解析错误时提示的脚本名字
ARGS=`getopt -o h --long help,no-quiet -n 'apmtest.sh' -- "$@"`
if [ $? != 0 ]; then
    print_usage
    exit 1
fi

#echo $ARGS
#将规范化后的命令行参数分配至位置参数（$1,$2,...)
eval set -- "${ARGS}"

while true
do
    case "$1" in
        -h|--help)
            print_usage
            exit
            ;;
        --no-quiet)
            OPT_QUIET=0
            shift
            ;;
        --)
            shift
            break
            ;;
        *)
            echo "Internal error!"
            exit 1
            ;;
    esac
done

rm -f apmtest*.report

test_modules

rm -f ${TMP_LOG}

if [[ ! -e ${REPORT_FILE} ]]; then
    echo "apmtest totally passed" >> ${REPORT_FILE}
fi

echo -e "\n\n\n${KCYAN}###############################"
echo -e "##                           ##"
echo -e "##      ApmTest Summary:     ##"
echo -e "##                           ##"
echo -e "###############################${RESET}"
cat ${REPORT_FILE} | sed "s|failed|${KRED}failed${RESET}|" | sed "s|passed|${KGREEN}passed${RESET}|"
